package com.leetcode.problems038;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/count-and-say/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {
    public String countAndSay(int n) {
        String val = String.valueOf(1);
        for (int i = 1; i < n ; i++) {
            val = countAndSay(String.valueOf(val));
        }
        return val;
    }


    public String countAndSay(String str) {
        StringBuilder res = new StringBuilder();
        int len = str.length();
        int startIndex = 0;
        for (int i = 1; i < len; i++) {
            if(str.charAt(i) != str.charAt(startIndex)){
               res.append(i - startIndex);
               res.append(str.charAt(startIndex));
               startIndex = i;
            }
        }

        res.append(len - startIndex);
        res.append(str.charAt(startIndex));
        
        return res.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution().countAndSay(6));;
    }
}
